package com.jh.paymentchannel.service;

import cn.jh.common.utils.CommonConstants;
import com.jh.paymentchannel.business.TopupPayChannelBusiness;
import com.jh.paymentchannel.pojo.TopupPayChannelRoute;
import com.jh.paymentchannel.util.WithDrawOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@Controller
@EnableAutoConfiguration
public class PayService {

	
	private static final Logger log = LoggerFactory.getLogger(PayService.class);
	
	
	@Autowired
	private TopupPayChannelBusiness  topupPayChannelBusiness;
	
	
	@Autowired
	private PaymentChannelFactory factory;
	
	/***请求url, 并返回跳装的url*/
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/pay/request")
	public @ResponseBody Object payrequest(HttpServletRequest request, 
			@RequestParam(value = "ordercode") String ordercode,
			@RequestParam(value = "cardno") String cardno,
			@RequestParam(value = "username") String username,
			@RequestParam(value = "amount") String amount,
			@RequestParam(value = "bankname") String bankname,
			@RequestParam(value = "phone") String phone,						
			@RequestParam(value = "pri_or_pub", required = false, defaultValue = "0") String priOrpub,			
			@RequestParam(value = "brandcode", required = false, defaultValue = "0") String brandcode,//品牌id
			@RequestParam(value = "channel_type", required = false, defaultValue = "2") String channelType,	//0充值1代付
			@RequestParam(value = "channel_tag", required = false, defaultValue = "YILIAN") String channelTag//渠道标识
			){
		
		// 目前不支持任何通道
		Map<String, Object> map = new HashMap<>(2);
		map.put(CommonConstants.RESP_CODE, CommonConstants.ERROR_WITHDRAW_ORDER_FAIL);
		map.put(CommonConstants.RESP_MESSAGE, "下单失败");
		return map;
		
//		if(!Tools.checkAmount(amount)){
//
//			map.put(CommonConstants.RESP_CODE,CommonConstants.ERROR_AMOUNT_ERROR);
//			map.put(CommonConstants.RESP_MESSAGE, "支付金额有错");
//			return map;
//		}
		
		
//		TopupPayChannelRoute payChannelRoute = topupPayChannelBusiness.getTopupChannelByBrandcode(brandcode, channelType, channelTag);
//		URI uri = util.getServiceUrl("user", "error url request!");
//		String url = uri.toString() + "/v1.0/user/channel/query";
//
//		/* 根据的渠道标识或去渠道的相关信息*/
//		MultiValueMap<String, String> requestEntity  = new LinkedMultiValueMap<String, String>();
//		requestEntity.add("channel_tag", channelTag);
//		RestTemplate restTemplate=new RestTemplate();
//		String resultObjx = restTemplate.postForObject(url, requestEntity, String.class);
//		JSONObject jsonObject =  JSONObject.fromObject(resultObjx);
//		JSONObject resultObj  =  jsonObject.getJSONObject("result");
//		String channelid  = resultObj.getString("id");
//
//
//		uri = util.getServiceUrl("user", "error url request!");
//		url = uri.toString() + "/v1.0/user/brandrate/query";
//		/* 根据的渠道标识或去渠道的相关信息*/
//		requestEntity  = new LinkedMultiValueMap<>();
//		requestEntity.add("brand_id", brandcode);
//		requestEntity.add("channel_id", channelid);
//		restTemplate=new RestTemplate();
//		resultObjx = restTemplate.postForObject(url, requestEntity, String.class);
//		jsonObject =  JSONObject.fromObject(resultObjx);
//		resultObj  =  jsonObject.getJSONObject("result");
//		String  withdrawFee  = resultObj.getString("withdrawFee");
		
	}
	
	
	
	
	
	
	/**代付结果查询*/
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/pay/query")
	public @ResponseBody Object payquery(HttpServletRequest request, 
			@RequestParam(value = "ordercode") String ordercode,
			@RequestParam(value = "brandcode", required = false, defaultValue = "0") String brandcode,
			@RequestParam(value = "channel_type", required = false, defaultValue = "2") String channelType,	
			@RequestParam(value = "channel_tag", required = false, defaultValue = "YILIAN") String channelTag){
	
			Map map = new HashMap();
			TopupPayChannelRoute payChannelRoute = topupPayChannelBusiness.getTopupChannelByBrandcode(brandcode, channelType, channelTag);
			PayRequest  paymentRequest = factory.getPayChannelRequest(payChannelRoute.getTargetChannelTag());
			log.info("paymentRequest--RESULT================"+paymentRequest);
			WithDrawOrder   withdrawOrder = paymentRequest.queryPay(ordercode);
			log.info("withdrawOrder--RESULT================"+withdrawOrder);
			if(withdrawOrder.getReqcode().equalsIgnoreCase(CommonConstants.SUCCESS)){
				map.put(CommonConstants.RESP_CODE,CommonConstants.SUCCESS);
				map.put(CommonConstants.RESULT, withdrawOrder);
				map.put(CommonConstants.RESP_MESSAGE, "交易成功");
			}else{
				map.put(CommonConstants.RESP_CODE, CommonConstants.ERROR_WITHDRAW_ORDER_FAIL);
				map.put(CommonConstants.RESULT, withdrawOrder);
				map.put(CommonConstants.RESP_MESSAGE, "交易失败");
			}
			
			return map;
		
	}
	
	
	
	
	
}
